package org.jeecg.modules.chanpSku.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.chanp.service.IKuybChanpService;
import org.jeecg.modules.chanpPinp.entity.KuybChanpPinp;
import org.jeecg.modules.chanpPinp.service.IKuybChanpPinpService;
import org.jeecg.modules.chanpSku.entity.KuybChanpSku;
import org.jeecg.modules.chanpSku.service.IKuybChanpSkuService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 产品规格型号
 * @Author: jeecg-boot
 * @Date: 2021-10-10
 * @Version: V1.0
 */
@Api(tags = "产品规格型号")
@RestController
@RequestMapping("/chanpSku/kuybChanpSku")
@Slf4j
public class KuybChanpSkuController extends JeecgController<KuybChanpSku, IKuybChanpSkuService> {
    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private IKuybChanpSkuService kuybChanpSkuService;

    @Autowired
    IKuybChanpService kuybChanpService;

    @Autowired
    IKuybChanpPinpService kuybChanpPinpService;

    /**
     * 分页列表查询
     *
     * @param kuybChanpSku
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "产品规格型号-分页列表查询")
    @ApiOperation(value = "产品规格型号-分页列表查询", notes = "产品规格型号-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(KuybChanpSku kuybChanpSku,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "99") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<KuybChanpSku> queryWrapper = QueryGenerator.initQueryWrapper(kuybChanpSku, req.getParameterMap());
        Page<KuybChanpSku> page = new Page<KuybChanpSku>(pageNo, pageSize);
        IPage<KuybChanpSku> pageList = kuybChanpSkuService.page(page, queryWrapper);
        pageList.getRecords().forEach(item -> {
            KuybChanpSku kuybChanpSkuParent = kuybChanpSkuService.getById(item.getParentid());
            if (kuybChanpSkuParent != null) {
//				KuybChanp kuybChanp = kuybChanpService.getById(kuybChanpSkuParent.getChanp());
//				if (kuybChanp != null) {
//					String toufdq = sysDictService.queryDictTextByKey("toufdq", kuybChanpSkuParent.getToufdq());
//					if (toufdq != null)
//						item.setParentName(kuybChanp.getChanpmc() + " ;" + toufdq + " ;" + kuybChanpSkuParent.getSkuname());
//				}
                item.setParentName(kuybChanpSkuParent.getSkuname());
            }
        });

        if(kuybChanpSku.getFrom() != null && kuybChanpSku.getFrom().equals("1")) {
			List<KuybChanpSku> newList = new ArrayList();
			// 过滤掉品牌专属产品
			pageList.getRecords().forEach(item -> {
				KuybChanpPinp kuybChanpPinp = kuybChanpPinpService.getOne(new LambdaQueryWrapper<KuybChanpPinp>().eq(KuybChanpPinp::getChanpId, item.getSkuvalue()), false);
				// 不匹配的才添加
				if(kuybChanpPinp == null)
					newList.add(item);
			});

			List<KuybChanpSku> newList2 = new ArrayList();
			// 添加品牌专属产品
			List<KuybChanpPinp> list = kuybChanpPinpService.list(new LambdaQueryWrapper<KuybChanpPinp>().eq(KuybChanpPinp::getPinpId, kuybChanpSku.getPinp()));
			if (list != null && list.size() > 0) {
				list.forEach(item -> {
					pageList.getRecords().forEach(item2 -> {
						if (item.getChanpId().equals(item2.getSkuvalue()))
							newList2.add(item2);
					});
				});
			}

			if (newList.size() > 0)
				pageList.setRecords(newList);
			if (newList2.size() > 0)
				pageList.setRecords(newList2);
		}

        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param kuybChanpSku
     * @return
     */
    @AutoLog(value = "产品规格型号-添加")
    @ApiOperation(value = "产品规格型号-添加", notes = "产品规格型号-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody KuybChanpSku kuybChanpSku) {
        kuybChanpSkuService.save(kuybChanpSku);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param kuybChanpSku
     * @return
     */
    @AutoLog(value = "产品规格型号-编辑")
    @ApiOperation(value = "产品规格型号-编辑", notes = "产品规格型号-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody KuybChanpSku kuybChanpSku) {
        kuybChanpSkuService.updateById(kuybChanpSku);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "产品规格型号-通过id删除")
    @ApiOperation(value = "产品规格型号-通过id删除", notes = "产品规格型号-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        kuybChanpSkuService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "产品规格型号-批量删除")
    @ApiOperation(value = "产品规格型号-批量删除", notes = "产品规格型号-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.kuybChanpSkuService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "产品规格型号-通过id查询")
    @ApiOperation(value = "产品规格型号-通过id查询", notes = "产品规格型号-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        KuybChanpSku kuybChanpSku = kuybChanpSkuService.getById(id);
        if (kuybChanpSku == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(kuybChanpSku);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param kuybChanpSku
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, KuybChanpSku kuybChanpSku) {
        return super.exportXls(request, kuybChanpSku, KuybChanpSku.class, "产品规格型号");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, KuybChanpSku.class);
    }

}
